#include "ImageManager.h"
#include <assert.h>
#include <iostream>

ImageManager::ImageManager(void){}
ImageManager::~ImageManager(void)
{
	/*for(std::vector<sf::Image>::iterator i = _ListImage.begin();
		i != _ListImage.end(); ++i)
		if( *i != NULL ) delete *i;*/
}

///Load an image from file
sf::Image& ImageManager::CreateImageFromFile(AppImage pImage, int pIndex, std::string pFileName)
{
	sf::Image *vLoadedImage;
	assert(pImage == pIndex);
	if(!(vLoadedImage = new sf::Image())->LoadFromFile(pFileName))
	{
		std::cout<<"An error occured while loading the image "<< pFileName<<std::endl;
		throw;
	}
	return *vLoadedImage;
}


/// Initialize the image manager
/// Must be invoked before using it!
void ImageManager::LoadImages()
{
	//Check list of images is empty
	if(_ListImage.size() != 0) return;

	//Load images
	_ListImage.push_back(CreateImageFromFile(Pacman,		0,	AppImage_Pacman));
	_ListImage.push_back(CreateImageFromFile(Ghost,			1,	AppImage_Ghost));
	_ListImage.push_back(CreateImageFromFile(Stone1,		2,	AppImage_Stone1));
	_ListImage.push_back(CreateImageFromFile(TitlePacman,	3,	AppImage_TitlePacman));
	_ListImage.push_back(CreateImageFromFile(Apple,			4,	AppImage_Apple));

	//Set loaded status
	_Loaded = true;
}

//Get an image
sf::Image& ImageManager::GetImage(AppImage pImage)
{
	//Check loaded status
	if(!_Loaded) LoadImages();
	return *( new sf::Image(_ListImage[pImage]) );
}